package mypractice;

import java.util.Arrays;

public class Leetcode169 {
    public static void main(String[] args) {
        int[] nums= new int[]{-1,1,1,1,2,1};
        majorityElement(nums);
    }

    public static int majorityElement(int[] nums) {
        Arrays.sort(nums);
//        System.out.println(Arrays.toString(nums));
        int mid=nums.length/2;
        int count=0;
        int i=0;
        int temp=nums[0];
        for (i=0;i< nums.length;i++) {
            if(nums[i]==temp){
                count++;
            }else {
                if(count>nums.length/2){
//                    System.out.println(temp);
                    return temp;
                }
                count=1;
                temp=nums[i];
            }
        }
//        System.out.println(temp);
        return temp;
    }
}
